package com.edu.activemq;

import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;

import com.edu.common.constant.QUEUE_TYPE;
import com.edu.common.utils.JsonUtils;

//@Component
//@EnableJms
public class ConsumerService extends MessageListenerAdapter {

	private static final Logger log = LoggerFactory.getLogger(ConsumerService.class);

	@SuppressWarnings("unchecked")
	@JmsListener(destination = QUEUE_TYPE.QUEUE_TEST, concurrency = "5-10")
	public void onMessage(Message message, Session session) throws JMSException {
		try {
			Map<String,Object> bean = (Map<String,Object>) getMessageConverter().fromMessage(message);
			System.out.println(JsonUtils.obj2json(bean));
			message.acknowledge();
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	@JmsListener(destination = QUEUE_TYPE.QUEUE_SMS, concurrency = "5-10")
	public void onMessageSms(Message message, Session session) throws JMSException {
		try {
			Map<String,Object> bean = (Map<String,Object>) getMessageConverter().fromMessage(message);
			System.out.println(JsonUtils.obj2json(bean));
			message.acknowledge();
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	@JmsListener(destination = QUEUE_TYPE.QUEUE_EMAIL, concurrency = "5-10")
	public void onMessageEmail(Message message, Session session) throws JMSException {
		try {
			Map<String,Object> bean = (Map<String,Object>) getMessageConverter().fromMessage(message);
			System.out.println(JsonUtils.obj2json(bean));
			message.acknowledge();
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	@JmsListener(destination = QUEUE_TYPE.QUEUE_ORDER, concurrency = "5-10")
	public void onMessageOrder(Message message, Session session) throws JMSException {
		try {
			Map<String,Object> bean = (Map<String,Object>) getMessageConverter().fromMessage(message);
			System.out.println(JsonUtils.obj2json(bean));
			message.acknowledge();
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
	}
}